﻿define(function (require) {
    var service = require('ezdesk/utility/service');
    require('lib/jquery-cookie');

    var BoxStauts = {
        'Login': {
            'On': function () {
                $("#auto-login").attr("checked", true);

                $("#lbl-login")
                    .addClass('ui-checkbox-on')
                    .removeClass('ui-checkbox-off');

                $("#lbl-login").find('.ui-icon')
                    .removeClass('ui-icon-checkbox-off')
                    .addClass('ui-icon-checkbox-on');

            },
            'Off': function () {
                $("#auto-login").attr("checked", false);

                $("#lbl-login")
                    .addClass('ui-checkbox-off')
                    .removeClass('ui-checkbox-on');

                $("#lbl-login").find('.ui-icon')
                    .removeClass('ui-icon-checkbox-on')
                    .addClass('ui-icon-checkbox-off');
            }
        },
        'User': {
            'On': function () {
                $("#remember-user").attr("checked", true);

                $("#lbl-user")
                    .addClass('ui-checkbox-on')
                    .removeClass('ui-checkbox-off');

                $("#lbl-user").find('.ui-icon')
                                .removeClass('ui-icon-checkbox-off')
                                .addClass('ui-icon-checkbox-on');
            },
            'Off': function () {
                $("#remember-user").attr("checked", false);

                $("#lbl-user")
                    .addClass('ui-checkbox-off')
                    .removeClass('ui-checkbox-on');

                $("#lbl-user").find('.ui-icon')
                    .removeClass('ui-icon-checkbox-on')
                    .addClass('ui-icon-checkbox-off');

            }
        }
    }

    function loginCallback(statusInfo) {
        if (statusInfo.Status == 'Success') {
            $.postMessage('login:1', '*', parent);
            localStorage.setItem("isLogin", "true");
        }
        else {
            $('#loginpanel').show();
            $('#loginpanel-waiting').hide();
        }
    }

    function signIn(name, password) {
        $('#error').hide();
        $('#remind').show();
        $('#loginpanel').hide();
        $('#loginpanel-waiting').show();

        $('#txt-userName').html("用户： " + name);
        $('#txt-loginStatus').html("正在登陆..");

        service.Login(loginCallback, { userName: name, password: password });
    }

    function autoLogin() {
        var name = localStorage.getItem('userName');
        var password = localStorage.getItem('password');

        signIn(name, password);
    }

    function submitLogin() {
        var name = $('#txt-name').val();
        var password = $('#txt-password').val();

        if ($("#remember-user").attr("checked")) {
            localStorage.setItem('userName', name);
            localStorage.setItem('password', password);
        }

        signIn(name, password);
    };

    function checkIsOutOfPeriod() {
        return true;
    };


    function initEvent() {
        $("#auto-login").change(function () {
            if ($("#auto-login").attr("checked")) {
                localStorage.setItem('auto-login', 'true');
                localStorage.setItem('remember-user', 'true');

                BoxStauts.Login.On();
                BoxStauts.User.On();
            } else {
                localStorage.setItem('auto-login', 'false');

                BoxStauts.Login.Off();
            }

        })

        $("#remember-user").change(function () {
            if (!$("#remember-user").attr("checked")) {
                BoxStauts.Login.Off();
                BoxStauts.User.Off();

                localStorage.setItem('remember-user', 'false');
                localStorage.setItem('auto-login', 'false');

            } else {
                BoxStauts.User.On();

                localStorage.setItem('remember-user', 'true');
            }

        })

        $('#txt-name').keydown(function (e) {
            if (e.keyCode == 13) {
                $('#txt-password').focus();
            }
        });

        $('#txt-password').keydown(function (e) {
            if (e.keyCode == 13) {
                submitLogin();
            }
        });

        $('.ui-checkbox').keydown(function (e) {
            if (e.keyCode == 13) {
                submitLogin();
            }
        });

        $('#btn-login').click(function () {
            submitLogin();
        });

        $('#btn-login-again').click(function () {
            $('#loginpanel').show();
            $('#loginpanel-waiting').hide();
        });
    }

    $(function () {
        //记住密码 

        localStorage.setItem("isLogin", "false");

        if (localStorage.getItem("remember-user") == "true") {
            BoxStauts.User.On();

            $("#txt-name").val(localStorage.getItem("userName"));
            $("#txt-password").val(localStorage.getItem("password"));
        }

        if (localStorage.getItem("auto-login") == "true") {
            BoxStauts.User.On();
            BoxStauts.Login.On();

            autoLogin();
        } else {
            $('#loginpanel').show();
        }

        $.receiveMessage(function (e) {
            var paramKeyRegex = /\w*(?=\:)/g;
            var key = paramKeyRegex.exec(e.data);
            key = key ? key[0] : e.data;
            var params = e.data.substring(key.length + 1).split('|');

            if (key == 'signout') {
                $('#txt-loginStatus').html("正在注销..");

                service.SignOut(function (e) {
                    localStorage.setItem('auto-login', 'false');
                    localStorage.setItem("isLogin", "false");
                    $('#loginpanel').show();
                    $('#loginpanel-waiting').hide();
                });
            } else {
                $.postMessage(e.data, '*', parent);
            }
        });


        //是否超过试用有效期
        var checkIsOutOfPeriod = function () {
            var IsOutOfPeriod = service.CheckIsOutOfPeriod_Sync();
            return IsOutOfPeriod.Data !== undefined ? IsOutOfPeriod.Data : true;
        };

        if (!checkIsOutOfPeriod()) {
            initEvent();
        } else {
            $('#isOutOfPeriod').show();
            $('#btn-login')
                .unbind('click');
            $('#btn_fields').remove();
        }
    });

});
